package com.widget;

import java.util.LinkedList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.util.DensityUtil;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;

@SuppressLint("DrawAllocation")
public class GoalView extends View {
	Context mContext;
	int STEP = 5;
	
	public GoalView(Context context) {
		super(context);
		mContext = context;
	}
	
	public GoalView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
	}

	public GoalView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
	}
	/*
	public void setSelected(boolean show)
	{
		mSelected = show;
		this.invalidate();
	}
*/
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if(mJSONArray == null) return;

		Paint p = new Paint();
		p.setAntiAlias(true);
		p.setStyle(Paint.Style.STROKE);
		p.setColor(Color.parseColor("#fa7597"));
		p.setStrokeWidth(DensityUtil.dip2px(mContext, 2));
		
//		drawCurve(canvas,p,points);
		
		int count = points.size();
		for(int i = 0; i < count - 1; i++)
		{
	        canvas.drawLine(points.get(i).x, points.get(i).y, points.get(i + 1).x, points.get(i + 1).y, p);// 斜线  
		}
	}
	

	JSONArray mJSONArray = null;
	List<Point> points;
	
	List<Integer> points_x;
	List<Integer> points_y;
	
	private void drawCurve(Canvas canvas,Paint paint,List<Point> point) {
		Path curvePath = new Path();
		
		points_x.clear();
		points_y.clear();
		for (int i = 0; i < point.size(); i++) {
			points_x.add(point.get(i).x);
			points_y.add(point.get(i).y);
		}

		List<Cubic> calculate_x = calculate(points_x);
		List<Cubic> calculate_y = calculate(points_y);
		curvePath.moveTo(calculate_x.get(0).eval(0), calculate_y.get(0).eval(0));
		
		for (int i = 0; i < calculate_x.size(); i++) {
			for (int j = 1; j <= STEP; j++) {
				float u = j / (float) STEP;
				curvePath.lineTo(calculate_x.get(i).eval(u), calculate_y.get(i)
						.eval(u));
			}
		}
		
//		curvePath.close();
		canvas.drawPath(curvePath, paint);
	}
	
	private List<Cubic> calculate(List<Integer> x) {
		int n = x.size() - 1;
		float[] gamma = new float[n + 1];
		float[] delta = new float[n + 1];
		float[] D = new float[n + 1];
		int i;

		gamma[0] = 1.0f / 2.0f;
		for (i = 1; i < n; i++) {
			gamma[i] = 1 / (4 - gamma[i - 1]);
		}
		gamma[n] = 1 / (2 - gamma[n - 1]);

		delta[0] = 3 * (x.get(1) - x.get(0)) * gamma[0];
		for (i = 1; i < n; i++) {
			delta[i] = (3 * (x.get(i + 1) - x.get(i - 1)) - delta[i - 1])
					* gamma[i];
		}
		delta[n] = (3 * (x.get(n) - x.get(n - 1)) - delta[n - 1]) * gamma[n];

		D[n] = delta[n];
		for (i = n - 1; i >= 0; i--) {
			D[i] = delta[i] - gamma[i] * D[i + 1];
		}

		/* now compute the coefficients of the cubics */
		List<Cubic> cubics = new LinkedList<Cubic>();
		for (i = 0; i < n; i++) {
			Cubic c = new Cubic(x.get(i), D[i], 3 * (x.get(i + 1) - x.get(i))
					- 2 * D[i] - D[i + 1], 2 * (x.get(i) - x.get(i + 1)) + D[i]
					+ D[i + 1]);
			cubics.add(c);
		}
		return cubics;
	}
	
	public class Cubic {

		float a, b, c, d; /* a + b*u + c*u^2 +d*u^3 */

		public Cubic(float a, float b, float c, float d) {
			this.a = a;
			this.b = b;
			this.c = c;
			this.d = d;
		}

		/** evaluate cubic */
		public float eval(float u) {
			return (((d * u) + c) * u + b) * u + a;
		}
	}
	
	public void setPoint(JSONArray jsonArray,int height,int width,int milk,int index)
	{
		if(points == null)
		{
			points = new LinkedList<Point>();

			points_x = new LinkedList<Integer>();
			points_y = new LinkedList<Integer>();
		}
		
		points.clear();
		
		mJSONArray = jsonArray;
		
		JSONObject json = mJSONArray.getJSONObject(index);
		
		JSONArray array = json.getJSONArray("goalData");
		int count = array.size() + 1;
		int offset = width/(count - 1);
		
		for(int i = 0; i < count; i++)
		{
			if(i == count - 1)
			{
				if(index < mJSONArray.size() - 1)
				{
					JSONArray list = mJSONArray.getJSONObject(index + 1).getJSONArray("goalData");
					if(list.size() > 0)
					{
						int goal = list.getJSONObject(0).getIntValue("goal_ml");
						if(goal > milk) goal = goal/5;
						
						int yPos = height - goal*height/milk;
						points.add(new Point(width, yPos));
						continue;
					}
				}

				int goal = array.getJSONObject(0).getIntValue("goal_ml");
				if(goal > milk) goal = goal/5;
				
				int yPos = height - goal*height/milk;
				points.add(new Point(width, yPos));
			}
			else
			{
				int goal = array.getJSONObject(i).getIntValue("goal_ml");
				if(goal > milk) goal = goal/5;
				
				int yPos = height - goal*height/milk;
				points.add(new Point(offset*i, yPos));
			}
		}
		
		invalidate();
	}
}
